package com.bowen.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.bowen.gulimall.product.entity.AttrEntity;
import com.bowen.gulimall.product.service.AttrAttrgroupRelationService;
import com.bowen.gulimall.product.service.AttrService;
import com.bowen.gulimall.product.service.CategoryService;
import com.bowen.gulimall.product.vo.AttrGroupRelationVo;
import com.bowen.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bowen.gulimall.product.entity.AttrGroupEntity;
import com.bowen.gulimall.product.service.AttrGroupService;
import com.bowen.common.utils.PageUtils;
import com.bowen.common.utils.R;


/**
 * 属性分组
 *
 * @author bowen
 * @email 1461236870@gmail.com
 * @date 2022-10-20 13:26:17
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;


    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 添加组的关联关系
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }


    /**
     * \
     * 展示关联关系
     *
     * @param attrgroupId
     * @return
     */
    @GetMapping("{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {

        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data", entities);
    }


    /**
     * \
     * 展示没有关联的关联关系
     *
     * @param attrgroupId
     * @return
     */
    @GetMapping("{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoReationAttr(params, attrgroupId);

        return R.ok().put("page", page);
    }

    @GetMapping("/{catLogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable Long catLogId) {
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCateLogId(catLogId);
        List<AttrGroupWithAttrsVo> collect = vos.stream().filter(w -> {
                    return w.getAttrs() != null;
                }
        ).collect(Collectors.toList());
        return R.ok().put("data", collect);
    }


    /**
     * 删除关联关系
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] atto) {
        attrService.deleteRelation(atto);
        return R.ok();
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catLogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable Long catLogId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catLogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] path = categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
